{extend name="../template/admin/head.html"}
{block name="title"}
文章分类_{__block__}
{/block}
{block name="content"}
<blockquote class="layui-elem-quote news_search">
<a class="layui-btn" id="tjclass">添加分类</a> 说明：默认分类不可以删除！否则会系统异常
  </blockquote>
  <div class="layui-form news_list">
      <table class="layui-table">
        <colgroup>
        <col width="0.5%">
        <col width="9%">
        <col width="9%">
        <col width="9%">
        <col width="10%">
        </colgroup>
        <thead>
        <tr>
          <th></th>
          <th style="text-align:left;">分类名称</th>
          <th>文章数</th>
          <th>查看</th>
          <th>操作</th>
        </tr> 
        </thead>
        <tbody class="news_content">
  <colgroup>
    <col width="150">
    <col width="200">
    <col>
  </colgroup>
  <tbody>
  {volist name="category" id="category"}
    <tr id="data{$category.id}">
      <td></td>
      <td id="catname{$category.id}">{$category.name}</td>
      <td>{$category.count}</td>
      <td>
      <a href="#"><i class="layui-icon layui-icon-layer"></i></a>
</td>
      <td><div class="layui-btn-group">
  <button class="layui-btn" onclick="catedit({$category.id})">编辑</button>
  <button class="layui-btn" onclick="catdel({$category.id})">删除</button>
</div></td>
    </tr>
  {/volist}
  </tbody>
</table>

        </tbody>
  </div>
  </div>

{/block}
{block name="jssrc"}
<script>
$('#tjclass').on('click', function(){
  layer.open({
    title:'添加文章分类',
    type: 1,
    area: ['320px', '150px'],
      shadeClose: true, //点击遮罩关闭
      content: ' \<\div style="margin-top:10px; padding-right:10px;">\<\label class="layui-form-label">分类名称：\<\/label>\<\div class="layui-input-block">\<\input type="text" class="layui-input newsName" lay-verify="required" placeholder="分类名称" id="ctname">\<\/div>\<\/div><button class="layui-btn" style="margin-top:10px; margin-left:220px;"lay-submit="submit" lay-filter="tjop" id="wrnav">添加</button>'

    });
});  
//添加分类
layui.use('form', function(){
  var form = layui.form;    
  //添加分类
  form.on('submit(tjop)', function(date) {
    //获取输入的值
    text = $('#ctname').val();
    if (text != "") {
      $.ajax({
        type:"POST",
        url:"{:URL('Artice/atadd')}",
        dataType:"json",
        data:{name:text},
        beforeSend: function () {
            // 禁用按钮防止重复提交
            $("#wrnav").attr({"disabled":"disabled"});
            $("#wrnav").addClass("layui-btn-disabled");
            $("#wrnav").html("添加中...");
            layer.load(1);
          },
          error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (XmlHttpRequest.responseText != "") {
              layer.msg('提交失败，请稍后尝试！');
              layer.closeAll('loading');
              $("#wrnav").removeClass("layui-btn-disabled");
              $("#wrnav").removeAttr("disabled");  
              $("#wrnav").html("添加");
            }
          },
          success:function(data){
            //1为修改成功
            //2为修改失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              $("#wrnav").removeClass("layui-btn-disabled");
              $("#wrnav").removeAttr("disabled");  
              $("#wrnav").html("添加");
              $("#selectop").append('<option value="'+date.id+'">'+text+'</option>');
              form.render(); //更新全部
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
              $("#wrnav").removeClass("layui-btn-disabled");
              $("#wrnav").removeAttr("disabled");  
              $("#wrnav").html("添加");
            }
          }
        });
      return false;
    }else{
      layer.msg('所有内容不能为空!');
      return false;
    }
  });
});
//删除分类
function catdel(id){
layer.confirm('确认删除此分类，此分类下所有文章都要被改为 [默认分类]？', {
  btn: ['确认','取消'] //按钮
}, function(){
  if (id != "") {
        $.ajax({
          type:"POST",
          url:"{:URL('Artice/catdel')}",
          dataType:"json",
          data:{id:id},
          beforeSend: function () {
            // 动画显示
            layer.load(1);
            },
           error: function (XmlHttpRequest, textStatus, errorThrown) {
              if (XmlHttpRequest.responseText != "") {
                  layer.closeAll('loading');
                  layer.msg('提交失败，请稍后尝试！');
              }
          },
          success:function(data){
            //1为删除成功
            //2为删除失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              $('#data'+id).remove();
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
            }
          }
        });
        return false;
    }else{
      layer.msg('ID值为空!');
      return false;
    }
});
}
//编辑框显示并传递值
function catedit(num){
  var id = num;
  var odlname = $('#catname'+num).html();
  var content= "<div style=\"margin-top:10px; padding-right:10px;\">";
  content+= "<label class=\"layui-form-label\">分类名称：</label>";
  content+= "<div class=\"layui-input-block\">";
  content+= "<input type=\"text\" class=\"layui-input newsName\" lay-verify=\"required\" placeholder=\"分类名称\" id=\"newname\" value="+odlname+"></div><br>";
  content+= "</div>";
  content+= "<button class=\"layui-btn kit-login-btn\" lay-submit=\"submit\" lay-filter=\"wrnav\" id=\"wrnav\" style=\" float: right;margin-right:10px;margin-top:5px;\">修改</button>";
  layer.open({
    title:'编辑分类',
    type: 1,
    area: ['300px', '180px'],
      shadeClose: true, //点击遮罩关闭
      content: content,

    });
    //编辑
    layui.use('form', function(){
      var form = layui.form;    
      form.on('submit(wrnav)', function(date) {
        var newname = $("#newname").val();
        if (newname!= "") {
          $.ajax({
            type:"POST",
            url:"{:URL('Artice/catedit')}",
            dataType:"json",
            data:{id:id,name:newname},
            beforeSend: function () {
            // 禁用按钮防止重复提交
            $("#wrnav").attr({"disabled":"disabled"});
            $("#wrnav").addClass("layui-btn-disabled");
            $("#wrnav").html("修改中...");
            layer.load(1);
          },
          error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (XmlHttpRequest.responseText != "") {
              layer.msg('提交失败，请稍后尝试！');
              layer.closeAll('loading');
              $("#wrnav").removeClass("layui-btn-disabled");
              $("#wrnav").removeAttr("disabled");  
              $("#wrnav").html("修改");
            }
          },
          success:function(data){
            //1为修改成功
            //2为修改失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              $("#wrnav").removeClass("layui-btn-disabled");
              $("#wrnav").removeAttr("disabled");  
              $("#wrnav").html("修改");
              setTimeout(function(){
                location.reload();
              },1000);
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
              $("#wrnav").removeClass("layui-btn-disabled");
              $("#wrnav").removeAttr("disabled");  
              $("#wrnav").html("修改");
            }
          }
        });
          return false;
        }else{
          layer.msg('所有内容不能为空!');
          return false;
        }
      });
    });

  }
</script>
{/block}